(function($) { 

    /* --------------------------
     * DOCUMENT LOAD
     * -------------------------- */
     
    $(document).ready(function() {
        // Additional admin interface UI tweaks
        if($('#domain-alias-form').length) {
            domain_list_columns();
        }
        
        if($('body').hasClass('page-node-add') || $('body').hasClass('page-node-edit')) {
            //domain_sticky_form_actions();
            domain_node_reference_classes();
        }
                
    });
    
    // Change column widths on the Domain Access list tables
    function domain_list_columns() {
        $('tbody td', '#domain-alias-form table.sticky-table').addClass('narrow-field');
        $('input.fluid', '#domain-alias-form table.sticky-table').each(function() {
            $(this).parents('td').removeClass('narrow-field').addClass('full-width-field');
        });
    }
    
    // Make the node add/edit form actions sticky
    function domain_sticky_form_actions() {
        
        // Offset of Column Side (Form Actions)
        var stickyTop = $('.column-side').offset().top;
        // Set default to 0
        var height_tweaks = 0;
        // If admin menu is present
        var admin_menu = ($('#admin-menu').length ? true : false);
        // If overlay is present
        var overlay = ($('#overlay').length ? true : false);
        
        if (admin_menu) {
            height_tweaks = $('#admin-menu').height();
        }
        
        // If overlay is present check if admin menu
        // is present in the parent document
        if (overlay) {
            var overlay_parent = parent.document;
            
            
            if ($('body', overlay_parent).hasClass('admin-menu')) {
                
                admin_menu = true;
                height_tweaks = $('#admin-menu', overlay_parent).height();
            }
        }
        
        // On scroll
        $(window).scroll(function(){  
            
            // Window position
            var windowTop = $(window).scrollTop();

            // If Window position is past Form Actions
            // position make Form Actions fixed (sticky)
            if (stickyTop < (windowTop + height_tweaks)) {
              $('.column-side').addClass('fixed');
            }
            // If the window position is the same as
            // the Form Actions position remove fixed
            else if (stickyTop == windowTop) {
                $('.column-side').removeClass('fixed');
            }
            else {
                $('.column-side').removeClass('fixed');
            }
        
        });
    }
    
    // Add class to Multiple Node References Instances
    function domain_node_reference_classes() {
    
        $('.field-widget-node-reference-autocomplete').each(function() {
        
            if ($('.form-item table', this).length) {
                $(this).addClass('multiple-references')
            } else {
                $(this).addClass('single-reference')
            }
            
            if ($('.dialog-links', this).length) {
                $(this).addClass('reference-dialog');
            }
        
        });
        
        if ($('.field-widget-node-reference-autocomplete').hasClass('single-reference')) {
            
            $('.field-widget-node-reference-autocomplete div:first-child').addClass('field-wrapper');
            
            $('.field-widget-node-reference-autocomplete .field-wrapper').append('<div class="clear" />');
        }
    }
    
}) (jQuery);